<?php
session_start();
 include_once("../DataProvider.php"); 
 
 	function showGroupGame($result1,$type)
	{
	
		while($data1 = mysql_fetch_array($result1))
		{
						?>
	<div class="pic" >
     <div  class="div<?php echo $type."_".$data1["GameID"]?>" style="background: url(<?php echo $data1["Artwork"]?>)no-repeat; float:left; margin-left:15px; margin-top:5px; background-size:100px 100px;">
	<img onmouseout="javascript:mouseout(this)" onmouseover="javascript:mouseover(this)" id="img<?php echo $type."_".$data1["GameID"] ?>" class="<?php if(isset($_SESSION['listGame']))
	 { 
	 	if(in_array($data1['GameID'],$_SESSION['listGame']))
		{
			echo 'Selected';
			
			}
			else
			{
				echo 'unSelect';
				}
		
		
	 }else echo 'unSelect';
	 ?>" alt="<?php echo $data1['GameName']; ?>" src="<?php if(isset($_SESSION['listGame']))
	 { 
	 	if(in_array($data1['GameID'],$_SESSION['listGame']))
		{
			echo 'img/mask175_check.png';
			
			}
			else
			{
				echo 'img/mask175_2.png';
				}
		
		
	 }else echo 'img/mask175_2.png';
	 ?>" height="100px" width="100px" style=" cursor: pointer;" onclick="javascript:add(this)" />
    </div>
    

    <div  class="textGame" title="<?php echo $data1['GameName']?>" ><span style="cursor:pointer;" onclick="javascript:loadScreenShoot('<?php echo $data1["GameID"]  ?>')">
    <?php 
    $tengame = "";
    if(strlen($data1["GameName"])>15)
    {
        $tengame = substr($data1["GameName"],0,15)."...";}
    else{ 
    $tengame = $data1["GameName"];} echo $tengame; ?>
    </span>
    <br />  <span style="color:#C33; font-size:12px"><?php echo number_format($data1["Price"], 0, '.', ','); ?> 
    </span>VNĐ&nbsp;&nbsp;<span style="color:#666;"><?php if($data1["Size"]>1000)	
	 		echo number_format($data1["Size"]/1000,2,".",","). " GB"; 
		else
			echo number_format($data1["Size"],2,".",",") . " MB";?>
    </span>
    </div>
    <div><a style="text-decoration:none;" href="chitietgame.php?id=<?php echo $data1['GameID']; ?>">
    <span style="font-size:13px; color:#FFFBF0;" title="<?php echo $data1["Notes"];  ?>"><?php echo substr($data1["Notes"],0,35)."..."; ?></span></a>
    </div>
</div>
<?php }?>
<div style=" top:15px; left:5px;"><img src="img/<?php if($type==1) echo "ribbon_new"; else if($type ==2) echo "ribbon_hot"; else echo "ribbon_tai" ?>.png" style=" position:absolute; left:5px; margin-top: -8px; z-index:999;"/></div>
	<?php	
	}
	//================End function showGame==================
//defind variable
//typeID 
//	1: new game
//	2: hot game
//	3: download
if(isset($_GET["flag"]))
{
	$type = $_GET["flag"];
	$page = $_GET["page"];
	$groupID = (int)$_GET["typeID"];
	
	GetGameByGroupID($groupID,$type,$page);
	
}
else

if(isset($_GET["typeID"]) && isset($_GET["page"]))
{
	$page = $_GET["page"];
	$typeID = (int)$_GET["typeID"];
	?>
	 <div class="groupGame" id="groupGame1" ><?php
	GetGameByGroupID(1,'1',$page);
	echo "</div>";
	?>
	 <div class="groupGame" id="groupGame2" ><?php
	GetGameByGroupID(2,'2',$page);
	echo "</div>";
	?>
	 <div class="groupGame" id="groupGame3" ><?php
	GetGameByGroupID(3,'3',$page);
	echo "</div>";
}

//===========get list game by categories===============
function GetGameByGroupID($typeID,$type,$page)
{

	if($typeID==1)//game new
	{
		$sql = "select * from tbl_games order by CreateDate DESC";
		
	}
	else
		if($typeID == 2)//game hot
		{
			$sql = "select * from tbl_games where Status = 1 order by LastModified DESC, CreateDate DESC";

		}
		else// downloaded
		{
			$sql = "select * from tbl_games order by Downloaded DESC";
			
		}
	$result = DataProvider::ExecuteQuery($sql);
	$tongsorecord = mysql_num_rows($result);
	$y = 20;
	$tongsotrang = ceil($tongsorecord/$y);
	if($page > $tongsotrang)
	{
		$page = $tongsotrang;
	}
	$start = ($page-1)*$y;
	
	if($tongsorecord == 0){
		echo "<span class='text5'>Không có game nào!</span>";	
	}else{
	?>

   

    <?php
	//$grp = "groupGame".$type;
		$stt = 0;
		if($typeID==1)//game new
	{

		$sql1 = "select * from tbl_games  order by CreateDate DESC limit $start,$y";
	}
	else
		if($typeID == 2)//game hot
		{
			
			$sql1 = "select * from tbl_games where Status = 1 order by LastModified DESC, CreateDate DESC limit $start,$y";
		}
		else// downloaded
		{

			$sql1 = "select * from tbl_games  order by Downloaded DESC limit $start,$y";
		}
		$res = DataProvider::ExecuteQuery($sql1);
		?>
		<table width="100%"><tr><td>
		<?php
		showGroupGame($res,$type);
		?>
        </td></tr><tr><td align="right">
        <div class="pagenav"><table cellpadding="3" cellspacing="1" border="0" style="background: none repeat scroll 0 0 #FFFFFF;
    border: 1px solid #B0BDC6;
    color: #000000; margin-right:20px;"><tr><td class="vbmenu_control">Trang <?php echo $page."/".$tongsotrang; ?>&nbsp;</td>
        <?php
        if($page == 1)
				{echo"<td class='alt2'><< Đầu </td>";}
				else
				{
				echo"<td class='alt1'><a style='text-decoration:none;' href='#1' onclick='javascript:loadGameByGroup($typeID,1,$type);'><< Đầu </a> </td>";}
				if($page-3>0)
				$i = $page-3;
				else
				$i = 1;
			for($i;$i<=$tongsotrang;$i++)
			{
				if($tongsotrang < 5)
				{
					if($i == $page)
					{
					 echo "<td class='alt2'>$i</td>";	
					}
					else
					{
						echo "<td class='alt1'><a style='text-decoration:none;' href='#$i' onclick='javascript:loadGameByGroup($typeID,$i,$type);'> $i</a>  </td>";	
					}
				}
				else
				{
					if($i<$page+4)
					{
						if($i == $page)
						{
						 echo "<td class='alt2'>$i</td>";	
						}
						else
						{
							echo "<td class='alt1'><a style='text-decoration:none;' href='#$i' onclick='javascript:loadGameByGroup($typeID,$i,$type);'> $i</a>  </td>";	
						}
					}
					else
					{
						echo "<td class='alt1'>...</td>";
						$temp = $i * 5;	
						if( $temp <= $tongsotrang)
						{
							if($temp == $page)
							{
								 echo "<td class='alt2'> $temp </td>";
							}
							else
							 echo "<td class='alt1'><a style='text-decoration:none;' href='$temp' onclick='javascript:loadGameByGroup($typeID,$temp,$type);'>$temp</a>  </td>";
							
							  break;
						}
						else
						{
							if($tongsotrang == $page)
							{
								 echo "<td class='alt2'> $tongsotrang </td>";
							}
							else
							 echo "<td class='alt1'><a style='text-decoration:none;' href='#$tongsotrang' onclick='javascript:loadGameByGroup($typeID,$tongsotrang,$type);'> $tongsotrang</a>  </td>";
							
							  break;
							
						}
					}
				}
			}
			if($page == $tongsotrang)
				{echo"<td class='alt2'> Cuối >></td>";}
				else
				{
				echo"<td class='alt1' ><a style='text-decoration:none;' href='#$tongsotrang' onclick='javascript:loadGameByGroup($typeID,$tongsotrang,$type);'> Cuối >> </a></td>";
				}
}?><td style="cursor: pointer;" id="pagenav.37" class="vbmenu_control" title="" > <img border="0" alt="" title="" src="images/menu_open.gif" onclick="showgopage()"><div class="vbmenu_popup" id="pagenav_menu<?php echo $typeID; ?>" style="position: absolute; z-index: 999999; clip: rect(auto, auto, auto, auto); display: none;">
		<table cellspacing="1" cellpadding="4" border="0">
		<tbody><tr>
			<td nowrap="nowrap" class="thead" style="background-color:#5C7099;">Go to Page...</td>
		</tr>
		<tr>
			<td class="vbmenu_option" title="">
			<form action='javascript:loadGameByGroup(<?php echo $typeID; ?>,document.gopage<?php echo $typeID; ?>.go.value, <?php echo $type; ?>)' method="get" onsubmit="return checkgo();" id="pagenav_form<?php echo $typeID; ?>" name="gopage<?php echo $typeID; ?>">
				<input type="text" class="bginput" id="pagenav_itxt<?php echo $typeID; ?>" name="go" style="font-size:11px" size="4">
				<input type="submit" class="button1" id="pagenav_ibtn<?php echo $typeID; ?>" value="Go">
			</form>
			</td>
		</tr>
		</tbody></table>
	</div></td>
</tr></table>
	</td></tr></table>

  
<?php

}
?>